<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>JUC相关 | Eetal&#39;s Blog</title>

  <!-- keywords -->
  

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="欢迎查看Eetal的第八篇博客–JUC相关 JUCJUC是java.util.concurrent的简写,该包下包含一系列java关于多线程协作相关的类 notify和waitnotify和wait为Object的方法,需要当前线程持有该对象锁,没有调用则会排除非法监管状态的异常,wait使得当前线程放弃该对象锁,进入条件等待队列,notify从该对象锁的条件等待队列中唤醒一个线程，使其进入对象锁">
<meta property="og:type" content="article">
<meta property="og:title" content="JUC相关">
<meta property="og:url" content="http://eetal.gitee.io/blog/2019/03/23/JUC相关/index.html">
<meta property="og:site_name" content="Eetal&#39;s Blog">
<meta property="og:description" content="欢迎查看Eetal的第八篇博客–JUC相关 JUCJUC是java.util.concurrent的简写,该包下包含一系列java关于多线程协作相关的类 notify和waitnotify和wait为Object的方法,需要当前线程持有该对象锁,没有调用则会排除非法监管状态的异常,wait使得当前线程放弃该对象锁,进入条件等待队列,notify从该对象锁的条件等待队列中唤醒一个线程，使其进入对象锁">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2019-03-26T00:32:10.084Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JUC相关">
<meta name="twitter:description" content="欢迎查看Eetal的第八篇博客–JUC相关 JUCJUC是java.util.concurrent的简写,该包下包含一系列java关于多线程协作相关的类 notify和waitnotify和wait为Object的方法,需要当前线程持有该对象锁,没有调用则会排除非法监管状态的异常,wait使得当前线程放弃该对象锁,进入条件等待队列,notify从该对象锁的条件等待队列中唤醒一个线程，使其进入对象锁">
  
    <link rel="alternative" href="/atom.xml" title="Eetal&#39;s Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="http://www.yangyitao.top/FiveChess/resource/ico/favicon.ico">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  
  

  <script src="//cdn.bootcss.com/require.js/2.3.2/require.min.js"></script>
  <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

  
</head>
<body>
  <div id="container">
    <div id="particles-js"></div>
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
	<header id="header" class="inner">
		<a href="/" class="profilepic">
			
			<img lazy-src="/blog/images/head.jpg" class="js-avatar">
			
		</a>

		<hgroup>
		  <h1 class="header-author"><a href="/">Eetal</a></h1>
		</hgroup>

		
		<p class="header-subtitle">java coder</p>
		

		
			<div class="switch-btn">
				<div class="icon">
					<div class="icon-ctn">
						<div class="icon-wrap icon-house" data-idx="0">
							<div class="birdhouse"></div>
							<div class="birdhouse_holes"></div>
						</div>
						<div class="icon-wrap icon-ribbon hide" data-idx="1">
							<div class="ribbon"></div>
						</div>
						
						<div class="icon-wrap icon-link hide" data-idx="2">
							<div class="loopback_l"></div>
							<div class="loopback_r"></div>
						</div>
						
						
					</div>
					
				</div>
				<div class="tips-box hide">
					<div class="tips-arrow"></div>
					<ul class="tips-inner">
						<li>菜单</li>
						<li>标签</li>
						
						<li>友情链接</li>
						
						
					</ul>
				</div>
			</div>
		

		<div class="switch-area">
			<div class="switch-wrap">
				<section class="switch-part switch-part1">
					<nav class="header-menu">
						<ul>
						
							<li><a href="/blog/">Home</a></li>
				        
							<li><a href="/blog/archives">Archives</a></li>
				        
						</ul>
					</nav>
					<nav class="header-nav">
						<div class="social">
							
						</div>
					</nav>
				</section>
				
				
				<section class="switch-part switch-part2">
					<div class="widget tagcloud" id="js-tagcloud">
						
					</div>
				</section>
				
				
				
				<section class="switch-part switch-part3">
					<div id="js-friends">
					
			          <a target="_blank" class="main-nav-link switch-friends-link" href="https://github.com/smackgg/hexo-theme-smackdown">smackdown</a>
			        
			        </div>
				</section>
				

				
			</div>
		</div>
	</header>				
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
  	<div class="overlay">
  		<div class="slider-trigger"></div>
  		<h1 class="header-author js-mobile-header hide">Eetal</h1>
  	</div>
	<div class="intrude-less">
		<header id="header" class="inner">
			<div class="profilepic">
				<img lazy-src="/blog/images/head.jpg" class="js-avatar">
			</div>
			<hgroup>
			  <h1 class="header-author">Eetal</h1>
			</hgroup>
			
			<p class="header-subtitle">java coder</p>
			
			<nav class="header-menu">
				<ul>
				
					<li><a href="/blog/">Home</a></li>
		        
					<li><a href="/blog/archives">Archives</a></li>
		        
		        <div class="clearfix"></div>
				</ul>
			</nav>
			<nav class="header-nav">
				<div class="social">
					
				</div>
			</nav>
		</header>				
	</div>
</nav>
      <div class="body-wrap"><article id="post-JUC相关" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/blog/2019/03/23/JUC相关/" class="article-date">
  	<time datetime="2019-03-23T01:36:36.204Z" itemprop="datePublished">2019-03-23</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      JUC相关
      
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
        

        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>欢迎查看Eetal的第八篇博客–JUC相关</p>
<h2 id="JUC"><a href="#JUC" class="headerlink" title="JUC"></a>JUC</h2><p>JUC是java.util.concurrent的简写,该包下包含一系列java关于多线程协作相关的类</p>
<h2 id="notify和wait"><a href="#notify和wait" class="headerlink" title="notify和wait"></a>notify和wait</h2><p>notify和wait为Object的方法,需要当前线程持有该对象锁,没有调用则会排除非法监管状态的异常,wait使得当前线程放弃该对象锁,进入条件等待队列,notify从该对象锁的条件等待队列中唤醒一个线程，使其进入对象锁的竞争队列</p>
<h2 id="可重入锁和不可重入锁区别"><a href="#可重入锁和不可重入锁区别" class="headerlink" title="可重入锁和不可重入锁区别"></a>可重入锁和不可重入锁区别</h2><p>可重入锁使得一个线程内执行的同锁方法之间的调用不需要重新获取锁，比如对象锁—某个对象中的实例方法的互相调用</p>
<h2 id="Lock相关"><a href="#Lock相关" class="headerlink" title="Lock相关"></a>Lock相关</h2><p>lock()方法请求锁,如果获取失败则阻塞直到获取成功<br>unLock()方法释放锁,需要拥有锁才可调用，否则抛出异常<br>tryLock()方法，尝试获取锁,不阻塞,立即返回，获取成功返回true，获取失败返回false</p>
<h2 id="Lock—Condition"><a href="#Lock—Condition" class="headerlink" title="Lock—Condition"></a>Lock—Condition</h2><p>通过lock.newCondition()方法获得，代表一个条件<br>类似于Object的notify和wait<br>condition.await() == object.wait()<br>condition.signal() == object.notify()</p>
<h2 id="Lock与synchonized"><a href="#Lock与synchonized" class="headerlink" title="Lock与synchonized"></a>Lock与synchonized</h2><p>synchonized不支持响应中断<br>Lock可以响应中断，但需要编码完成细节</p>
<h3 id="请移步"><a href="#请移步" class="headerlink" title="请移步"></a>请移步</h3><p>个人主页: <a href="http://yangyitao.top" target="_blank" rel="noopener">yangyitao.top</a></p>

      
    </div>
    
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog/2019/03/23/使用future机制和对象锁实现SynchronizedExecutor/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">&lt;</strong>
      <div class="article-nav-title">
        
          使用future机制和对象锁实现SynchronizedExecutor
        
      </div>
    </a>
  
  
    <a href="/blog/2018/07/17/SpringBootCache/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">SpringBoot在同一类中方法内部调用缓存方法Cacheable注解无效的问题</div>
      <strong class="article-nav-caption">&gt;</strong>
    </a>
  
</nav>

  
</article>


<div class="ds-share share" data-thread-key="JUC相关" data-title="JUC相关" data-url="http://eetal.gitee.io/blog/blog/2019/03/23/JUC相关/"  data-images="/blog/images/head.jpg" data-content="JUC相关">
    <div class="ds-share-inline">
      <ul  class="ds-share-icons-16">
      	<li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
        <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
        <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
        <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
        <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>
      </ul>
      <div class="ds-share-icons-more">
      </div>
    </div>
 </div>
 





</div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info">
      <div class="footer-left">
        &copy; 2019 Eetal
      </div>
        <div class="footer-right">
          <a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/smackgg/hexo-theme-smackdown" target="_blank">Smackdown</a>
        </div>
    </div>
  </div>
</footer>
    </div>
    
  <link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.css">


<script>
	var yiliaConfig = {
		fancybox: true,
		mathjax: true,
		animate: true,
		isHome: false,
		isPost: true,
		isArchive: false,
		isTag: false,
		isCategory: false,
		open_in_new: false
	}
</script>
<script src="/blog/js/main.js"></script>



<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>


  </div>
</body>
</html>